package com.jing.qq.qq;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.springfairytale.qq.common.Constant;
import com.springfairytale.qq.common.Protocol;

import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class Register extends AppCompatActivity {
    private EditText pwd;
    private EditText birthday;
    private EditText sex;
    private EditText nickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        pwd=(EditText)findViewById(R.id.pwd);
        birthday=(EditText)findViewById(R.id.birthday);
        sex=(EditText)findViewById(R.id.sex);
        nickname=(EditText)findViewById(R.id.nickname);
    }

    public void regist(View view) {
        new Thread(){
            @Override
            public void run() {

                try {
                    Socket socket=new Socket("192.168.1.102",8888);
                    Protocol pro = new Protocol("01");
                    pro.setAction(Constant.REGISTER);
                    Map<String,Object> msg = new HashMap<>();

                    String s = sex.getText().toString();
                    int se = Integer.parseInt(s);

                    msg.put("pwd",pwd.getText().toString());
                    msg.put("nickname",nickname.getText().toString());
                    msg.put("sex",se);
                    msg.put("birthday",birthday.getText().toString());

                    pro.setMsg(msg);
                    pro.sendMessage(socket);

                    Protocol repro =Protocol.getProtocol(socket);
                    String result=repro.getMsg().get("result").toString();

                    if("succ".equals(result)){
                        Log.i("liu","register success,u number:"+repro.getMsg().get("uid"));
                        // 往回传值 绑定intent
                        Intent intent=getIntent();
                        intent.putExtra("uid",repro.getMsg().get("uid").toString());
                        intent.putExtra("pwd",pwd.getText().toString());
                        setResult(7,intent);
                        finish();

                    }else{
                        Log.i("liu","failed,重新输入信息");
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();



    }


}
